package com.ken.mqspring.converter;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;



/**
 * 自定义参数类
 * Created by renchongyu on 2018/11/21.
 */
public class TextMessageConverter implements MessageConverter {


    @Override
    public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
        return new Message(o.toString().getBytes(),messageProperties);
    }

    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        System.out.println("filter message");
        String contentType = message.getMessageProperties().getContentType();
        if (contentType!=null && contentType.contains("text")){
            return new String(message.getBody());
        }
        return message.getBody();
    }
}
